package com.sistemagestion.clientes.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import com.sistemagestion.R;
import com.sistemagestion.BO.LocalidadBO;
import com.sistemagestion.Entities.Cliente;
import com.sistemagestion.Entities.Localidad;

public class ClienteAdapter extends ArrayAdapter<Cliente> {

	private List<Cliente> _clientes, allClientes;
	LocalidadBO localidadbo = new LocalidadBO();
	private int res;
	Context context = null;

	public ClienteAdapter(Context context, int textViewResourceId,
			List<Cliente> objects) {
		super(context, textViewResourceId, objects);
		this.res = textViewResourceId;
		this._clientes = objects;
		this.allClientes = objects;
		this.context = context;
	}

	@Override
	public int getCount() {
		return _clientes.size();
	}

	@Override
	public Filter getFilter() {
		return new ClienteFilter();
	}

	@Override
	public Cliente getItem(int position) {
		return _clientes.get(position);
	}

	@Override
	public void add(Cliente object) {
		_clientes.add(object);
		notifyDataSetChanged();
	}

	@Override
	public void remove(Cliente object) {
		allClientes.remove(object);
		_clientes.remove(object);
		notifyDataSetChanged();
	}

	public void sort(int criterio) {
		Collections.sort(_clientes, new ComparadorCliente(criterio));
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewholder;
		if (convertView == null) {
			viewholder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(res, null);
			viewholder.txtDireccion = (TextView) convertView
					.findViewById(R.id.txtDireccion);
			viewholder.txtLocalidad = (TextView) convertView
					.findViewById(R.id.txtLocalidad);
			viewholder.txtNombreApellido = (TextView) convertView
					.findViewById(R.id.txtNombreApellido);
			viewholder.color = (View) convertView.findViewById(R.id.color);
			convertView.setTag(viewholder);

		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		Cliente cli = (Cliente) getItem(position);
		Localidad loc = localidadbo.getById(cli.getIdLocalidad());

		viewholder.txtDireccion.setText(cli.getDireccion());
		viewholder.txtLocalidad.setText(loc.getNombreLocalidad());
		viewholder.txtNombreApellido.setText(cli.getNombreApellido());
		viewholder.color.setBackgroundResource(R.drawable.gradientgreen);

		// btnActivo.setChecked(art.isActivo());

		return convertView;
	}

	private static class ViewHolder {
		TextView txtNombreApellido;
		TextView txtDireccion;
		TextView txtLocalidad;
		View color;
	}

	public class ClienteFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {

			FilterResults filterResults = new FilterResults();
			List<Cliente> clientesFiltrados = new ArrayList<Cliente>();
			if (constraint != null && constraint.length() > 0) {

				constraint = constraint.toString().toLowerCase();
				for (Cliente cli : allClientes) {
					String text = cli.toString().toLowerCase();
					if (text.contains(constraint)
							&& text.startsWith((constraint).toString())) {
						clientesFiltrados.add(cli);
					}
				}
			} else {
				clientesFiltrados = allClientes;
			}

			filterResults.count = allClientes.size();
			filterResults.values = clientesFiltrados;
			return filterResults;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			_clientes = (List<Cliente>) results.values;
			notifyDataSetChanged();
		}
	}
}
